<?php


namespace app\admin\model;


use think\Db;

class TombareaInformationProductModel extends BaseModel
{
    protected $name='tombarea_information_product';


    public function doAddData($param){
        $col_id = explode(',',$param['col_id']);
        $id = explode(',',$param['id']);
        // 艺术墓 - 且 - 有order_id  修改其档案编号，档案编号不能重复
        if(!empty($param['order_id'])){
            $order_info = OrderModel::where(['order_id'=>$param['order_id']])->find();
            if ($order_info){
                if($param['archives_sn']){
                    $order_ex = OrderModel::where('order_id',"<>",$param['order_id'])->where(array('archives_sn'=>$param['archives_sn']))->select()->toArray();
                    if($order_ex){
                        $this->error = '档案编号已存在';
                        return false;
                    }else{
                        $o_data['archives_sn'] = $param['archives_sn'];
                        $res   =  OrderModel::where(['order_id'=>$param['order_id']])->update($o_data);
                    }
                }else{
                    $this->error = '档案编号不能为空';
                    return false;
                }
            }
        }

        foreach ($col_id as $key=>&$value){
//               $tomb_address = Db::name('tombarea_row_col')->where('colID',$value)->find();
            $tomb_address = TombareaRowColModel::alias('c')
                ->join('dj_tombarea_row r','  c.rowid = r.rowID')
                ->join('dj_tombarea t','r.tombarea_id = t.id')
                ->field('c.colID,c.num,c.rowid,r.tombarea_id,t.cate_id,c.reacher,t.cemetery_id,t.name tomb_address,c.rows_num tomb_loor,c.column_num tomb_bit_number ')
                ->where('c.colID',$value)->find();
            $tomb_region = $this->getSup($tomb_address['cemetery_id']);
            $region = '';
            $region_ceng = '';
            if (isset($param['layer_num'])){
                $region_ceng = $param['layer_num'];
            }else{
                $region_ceng = $tomb_address['tomb_loor'];
            }

            foreach ($tomb_region as $k=>&$v){
                if ($v['type']=='层'){
                    $region_ceng = $v['name'];
                }else{
                    $region = $v['name'].$region;
                }
            }
            $data =[
                'tomb_id'=>$value,
                'tomb_num'=>$tomb_address['num'],
                'tombar_id'=>$tomb_address['tombarea_id'],
                'cemetery_id'=>$tomb_address['cemetery_id'],
//                   'tomb_sn'=>$tomb_sn[$key],
                'cate_id'=>$param['cate_id'], //类型
                'pro_status'=>$param['pro_status'], //类型
                'funeral'=>isset($param['funeral'])?$param['funeral']:'',//墓穴规制
                'tomb_regulation'=>isset($param['tomb_regulation'])?$param['tomb_regulation']:'',
                'footprint'=>isset($param['footprint'])?$param['footprint']:'',//墓碑高度/空间高度/左右长度
                'tombstone_w'=>isset($param['tombstone_w'])?$param['tombstone_w']:'',//占地宽度/空间宽度/纵深宽度
                'tombstone_h'=>isset($param['tombstone_h'])?$param['tombstone_h']:'',//墓碑高度/空间高度/左右长度
                'single_cavity_size'=>!empty($param['sc_width'])?$param['sc_width'].'*'.$param['sc_height'].'*'.$param['sc_thick']:'',//单穴最大
                'convergence'=>isset($param['convergence'])?$param['convergence']:'',//收敛形式
                'specifications'=>isset($param['green_belt'])?$param['specifications']:'',//拜道规格
                'green_belt'=>isset($param['green_belt'])?$param['green_belt']:'', // 绿化带
                'greenbelt_num'=>isset($param['greenbelt_num'])?$param['greenbelt_num']:'',
                'maintain'=>isset($param['maintain'])?$param['maintain']:'',//墓穴维护期  ,
                'warranty'=>isset($param['warranty'])?$param['warranty']:'',//质保期
                'is_bye'=>isset($param['is_bye'])?$param['is_bye']:0,//1含拜道0不
                'limit_height'=>isset($param['limit_height'])?$param['limit_height']:'',//限制高度
                'design'=>isset($param['design'])?$param['design']:'',//设计墓穴
                'periphery'=>isset($param['periphery'])?$param['periphery']:'',//外围设置
                'current_make'=>isset($param['current_make'])?$param['current_make']:'',//当前制作
                'new_pro'=>isset($param['new_pro'])?$param['new_pro']:'',//新产品
                'old_pro'=>isset($param['old_pro'])?$param['old_pro']:'',//以旧换新产品
                'product_mb_base_id'=>isset($param['product_mb_base_id'])?$param['product_mb_base_id']:'',
                'product_mb_base_id1'=>isset($param['product_mb_base_id'])?$param['product_mb_base_id1']:'',
                'product_info_id'=>isset($param['product_info_id'])?$param['product_info_id']:'',
                'selling_price'=>isset($param['selling_price'])?$param['selling_price']:'',//售价
                'menu_type'=>isset($param['menu_type'])?$param['menu_type']:'',//售价单位
                'tomb_region'=>$region,//墓位所在区域
                'tomb_address'=>$tomb_address['tomb_address'],//所在位置
                'layer_num'=>$region_ceng,//所在层
                'tomb_bit_number'=>$tomb_address['tomb_bit_number'].'号位',//所在位号
                'add_user'=>cmf_get_current_admin_id(),
            ];
            if ($param['cate_id']==2){
                $data['tom_img']=$param['upload_plan'];
            }else{
                $data['tom_img']=$param['tom_img'];
            }
            if ($param['substitute']){

                Db::name('tombarea_row_col')->where(['colID'=>$value])->update(['reacher'=>$param['substitute']]);
            }else{
                Db::name('tombarea_row_col')->where(['colID'=>$value])->update(['reacher'=>'']);
            }
            Db::name("tomb_mb")->where(['tomb_id'=>$value,'type'=>0])->delete();
            if ($param['cate_id']==2) {
                if (isset($param['mb'])) {
                    foreach ($param['mb'] as $km => &$valuem) {
                        if (!empty($valuem['dele_info_mb_id'])) {
                            Db::name("tomb_mb")->where(['tomb_id' => $value, 'id' => $valuem['dele_info_mb_id'], 'type' => 0])->delete();
                        } else {
                            if (!empty($valuem['mb_id'])) {
                                $mb_info = [
                                    'tomb_id' => $value,
                                    'mb_id' => $valuem['mb_id'],
                                    'mb_name' => $valuem['mb_name'],
                                    'address' => $valuem['address'],
                                    'status' => 0,
                                    'type' => 0,
                                ];
                                if (!empty($valuem['mb_id_id'])) {
                                    Db::name("tomb_mb")->insertGetId($mb_info);
                                } else {
                                    Db::name("tomb_mb")->insertGetId($mb_info);
                                }
                            }
                        }
                    }
                }
                if (isset($param['other'])) {
                    foreach ($param['other'] as $ko => &$valueother) {
                        if (!empty($valueother['dele_product_info_id'])) {
//                           Db::name('product_mb_info')->where(['id' => $valueother['dele_product_info_id']])->delete();
                            Db::name("tomb_mb")->where(['tomb_id' => $value, 'mb_info_id' => $valueother['dele_product_info_id'], 'type' => 7])->delete();
                        } else {
                            $product_info = array(
                                'itype' => 7,
                                'mb_id' => $value,
                                'iname' => $valueother['iname'],
                                'iwidth' => $valueother['iwidth'],
                                'iheight' => $valueother['iheight'],
                                'ithick' => $valueother['ithick'],
                                'icate' => $valueother['icate'],
                                'inumber' => $valueother['inumber'],
                                'imaterial_price' => $valueother['imaterial_price'],
                                'irepair_price' => $valueother['irepair_price'],
                                'ichange_price' => $valueother['ichange_price'],
                                'isort' => $valueother['isort'],
                                'add_time' => time(),
                                'add_user' => cmf_get_current_admin_id(),
                            );
                            if (!empty($valueother['product_info_id'])) {
                                $res = Db::name('product_mb_info')->where('id', $valueother['product_info_id'])->update($product_info);
                                $mb_info_id = $valueother['product_info_id'];
                                $product_info_arr[] = $product_info;
                            } else {
                                $mb_info_id = Db::name('product_mb_info')->insertGetId($product_info);
                            }
                            if (!Db::name("tomb_mb")->where(['tomb_id' => $value, 'mb_info_id' => $mb_info_id, 'type' => 7])->find()) {
                                $mb_infos = [
                                    'tomb_id' => $value,
                                    'mb_id' => $valuem['mb_id'],
                                    'mb_name' => '',
                                    'mb_info_id' => $mb_info_id,
                                    'address' => $valuem['address'],
                                    'status' => 0,
                                    'type' => 7,
                                ];
                                Db::name("tomb_mb")->insertGetId($mb_infos);
                            }
                        }
                    }
                }
            }
            $information_info = Db::name("tombarea_information_product")->where(['tomb_id'=>$value])->find();
            if ($information_info){
                $res   =  Db::name("tombarea_information_product")->where(['id'=>$information_info['id']])->update($data);
            }else{
                if ($param['cate_id']==1){
                    Db::name('tombarea_row_col')->where(['colID'=>$value])->update(['col_status'=>0]);
                }else{
                    Db::name('tombarea_row_col')->where(['colID'=>$value])->update(['col_status'=>1]);
                }
                $data['add_time'] = time();
                $res   =  Db::name("tombarea_information_product")->insertGetId($data);
            }
        }
    }


}